package de.hwrBerlin.dataMining.view.widgets.table;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import de.hwrBerlin.dataMining.constants.ConfigConstants;

public class ExtendedTable {

	private Table _table;
	private TableShower _tableShower;

	private List<Object> _items;
	private String[] _header;

	public ExtendedTable(Composite pParent, int pStyle, TableShower pTableShower) {
		_table = new Table(pParent, pStyle);
		_tableShower = pTableShower;
	}

	public void setHeader(String[] pHeader) {
		if (!headerEquals(pHeader)) {
			_header = pHeader;
			for (TableColumn column : _table.getColumns()) {
				column.dispose();
			}
			for (String header : pHeader) {
				TableColumn column = new TableColumn(_table, SWT.NONE);
				column.setText(header);
				column.pack();
			}
		}
	}

	private boolean headerEquals(String[] pHeader) {
		if (pHeader == _header) {
			return true;
		}
		if (pHeader == null) {
			return false;
		}
		if (_header == null) {
			return false;
		}
		if (_header.length != pHeader.length) {
			return false;
		}
		boolean returnValue = true;
		for (int i = 0; i < pHeader.length; i++) {
			if (!_header[i].equals(pHeader[i])) {
				returnValue = false;
			}
		}
		return returnValue;
	}

	public String[] getHeader() {
		return _header;
	}

	public void setInput(Object[] pItems, int pHighlightedColumn) {
		_table.removeAll();
		_items = new ArrayList<Object>();
		
		Color red = new Color(_table.getDisplay(), ConfigConstants.TARGET_ATTRIBUTE_COLUMN_COLOR);
		
		for (Object item : pItems) {
			_items.add(item);
			List<String> input = _tableShower.getStringListFromRowObject(item);
			TableItem tableItem = new TableItem(_table, SWT.NONE);
			tableItem.setText(input.toArray(new String[0]));
			tableItem.setBackground(pHighlightedColumn, red);
		}
	}
	public void setInput(Object[] pItems) {
		_table.removeAll();
		_items = new ArrayList<Object>();
		
		for (Object item : pItems) {
			_items.add(item);
			List<String> input = _tableShower.getStringListFromRowObject(item);
			TableItem tableItem = new TableItem(_table, SWT.NONE);
			tableItem.setText(input.toArray(new String[0]));
		}
	}

	public Object getSelection() {
		int selection = _table.getSelectionIndex();
		if (selection == -1) {
			return null;
		}
		return _items.get(selection);
	}

	public void setSelection(Object pObjectToSelect) {
		int selection = _items.indexOf(pObjectToSelect);
		_table.setSelection(selection);
	}

	public Table getTable() {
		return _table;
	}

	public void setLayoutData(Object pLayout) {
		_table.setLayoutData(pLayout);
	}

	public void setColumns(String[] pAttributNames) {
		boolean isEquals = false;
		int counter = 0;
		if (pAttributNames.length == _table.getColumns().length) {
			isEquals = true;
			for (TableColumn column : _table.getColumns()) {
				if (column.getText() != pAttributNames[counter]) {
					isEquals = false;
				}
				counter++;
			}
		}
		if (!isEquals) {
			_table.removeAll();
			for (TableColumn column : _table.getColumns()) {
				column.dispose();
			}
			for (String attribut : pAttributNames) {
				TableColumn column = new TableColumn(_table, SWT.NONE);
				column.setText(attribut);
				
				column.pack();
			}
			_table.redraw();
		}
	}

}
